package com.example.sprintbootarithmetic.DataStructure.recursion;

public class MiGong {
    public static void main(String[] args) {
        int mum[][] = new int[8][7];
        for (int i = 0; i < 8; i++) {
            mum[i][0] = 1;
            mum[i][6] = 1;
        }
        for (int j = 0; j < 7; j++) {
            mum[0][j] = 1;
            mum[7][j] = 1;
        }
        mum[3][1] = 1;
        mum[3][2] = 1;
        res(mum, 1, 1, 1);
        int sum = 0;
        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 7; j++) {
                System.out.print(mum[i][j] + "\t");
                if (mum[i][j] == 2) {
                    sum++;
                }
            }
            System.out.println();
        }
        System.out.println("这里的sum总数为" + sum);
    }

    public static boolean res(int[][] map, int i, int j, int x) {
        if (map[6][5] == 2) {
            return true;
        } else {
            if (map[i][j] == 0) {
                map[i][j] = 2;
                boolean flag = false;
                switch (x) {
                    case 1:
                        if (res(map, i + 1, j, x) || res(map, i, j + 1, x) || res(map, i - 1, j, x) || res(map, i, j - 1, x)) {//下右上左走
                            flag = true;
                        } else {
                            map[i][j] = 3;//如果都不满足则为死路
                            flag = false;
                        }
                        break;
                    case 2:
                        if (res(map, i, j + 1, x) || res(map, i + 1, j, x) || res(map, i - 1, j, x) || res(map, i, j - 1, x)) //右下上左
                        {
                            flag = true;
                        } else {
                            map[i][j] = 3;//如果都不满足则为死路
                            flag = false;
                        }
                        break;
                    case 3:
                        if (res(map, i - 1, j, x) || res(map, i, j + 1, x) || res(map, i + 1, j, x) || res(map, i, j - 1, x)) {//上右下左
                            flag = true;
                        } else {
                            map[i][j] = 3;//如果都不满足则为死路
                            flag = false;
                        }
                        break;
                    case 4:
                        if (res(map, i, j - 1, x) || res(map, i - 1, j, x) || res(map, i, j + 1, x) || res(map, i + 1, j, x)) {//左上右下
                            flag = true;
                        } else {
                            map[i][j] = 3;//如果都不满足则为死路
                            flag = false;
                        }
                        break;
                }
                return flag;
            } else {
                return false;//如果是
            }
        }
    }

}
